﻿// global objects for getting WMI data
var refresher;
var CPU1Collection;

var dllCLSID = "{771CF1A6-FC96-45cf-B011-6469F0E56F64}";
var oShell;
var oFSO;
var Speedfan, regRoot;
var Temp1SensorString;

var LoadedBar = "stock_images\\st12.png";
var SemiLoadedBar = "stock_images\\st6.png";
var IdleBar = "stock_images\\st2.png";

function view_onOpen() 
{
	//system.perfmon.addCounter("\\Processor(0)\\% Processor Time", '');
	//system.perfmon.addCounter("\\Processor(1)\\% Processor Time", '');

	MEM_Total.innerText = parseInt(system.memory.totalPhysical/1000000) + "MB";

	var locator = new ActiveXObject("WbemScripting.SWbemLocator");
	strComputer = ".";
	var wmi_service =  locator.ConnectServer(".", "Root/Cimv2");
	wmi_service.Security_.ImpersonationLevel = 3;
	refresher = new ActiveXObject("WbemScripting.Swbemrefresher");
	CPU1Collection = refresher.AddEnum(wmi_service, "Win32_PerfFormattedData_PerfOS_Processor").objectSet;

	refresher.Refresh();

	//speed fan stuff
	oShell = new ActiveXObject("WScript.Shell");
	oFSO = new ActiveXObject("Scripting.FileSystemObject");

	activeDLL("HKEY_CURRENT_USER");
	activeDLL("HKEY_LOCAL_MACHINE");

	RegisterSpeedfan();

	LoadOptions();

	setInterval("UpdateUsage()", 1000 );
}

function view_onClose()
{
	UnregisterSpeedfan();
}

function UpdateUsage()
{
	var CPU1Value = 0;
	var CPU2Value = 0;
	var MEMValue = 0;
	var x;

        // refresh the WMI data, and load into a collection
        refresher.Refresh();
        CPU1col_enum = new Enumerator(CPU1Collection);
	
	//get processor 0 info
	x = CPU1col_enum.item();
	CPU1Value = parseInt(x.PercentProcessorTime);

	//get processor 1 info
	CPU1col_enum.moveNext();
	x = CPU1col_enum.item();
	CPU2Value = parseInt(x.PercentProcessorTime);

	//CPU1_Label.innerText = CPU1Collection.PercentProcessorTime + "%";
	//CPU1_Progress.value = CPU1col_enum.PercentProcessorTime;
	//CPU1col_enum.moveNext();
	//CPU2_Progress.value = Math.min(100, Math.max(0, CPU1col_enum.PercentProcessorTime));

	//CPU1Value = parseInt(system.perfmon.currentValue("\\Processor(0)\\% Processor Time"));
	//CPU2Value = parseInt(system.perfmon.currentValue("\\Processor(1)\\% Processor Time"));
	MEMValue = parseInt(system.memory.usedPhysical/system.memory.totalPhysical *100);

	if(CPU1Value > 90)
	{
		CPU1_Progress.fullImage = LoadedBar;
	}
	else if(CPU1Value > 49)
	{
		CPU1_Progress.fullImage = SemiLoadedBar;
	}
	else
	{
		CPU1_Progress.fullImage = IdleBar;
	}

	if(CPU2Value > 90)
	{
		CPU2_Progress.fullImage = LoadedBar;
	}
	else if(CPU2Value > 49)
	{
		CPU2_Progress.fullImage = SemiLoadedBar;
	}
	else
	{
		CPU2_Progress.fullImage = IdleBar;
	}

	if(MEMValue > 90)
	{
		MEM_Progress.fullImage = LoadedBar;
	}
	else if(MEMValue > 49)
	{
		MEM_Progress.fullImage = SemiLoadedBar;
	}
	else
	{
		MEM_Progress.fullImage = IdleBar;
	}

	//debug.trace(MEMValue);

	CPU1_Pct.innerText = CPU1Value+"%";
	CPU2_Pct.innerText = CPU2Value+"%";
	MEM_Pct.innerText  = MEMValue+"%";
	MEM_Used.innerText = parseInt(system.memory.usedPhysical/1000000) + "MB";

	CPU1_Progress.value = CPU1Value;
	CPU2_Progress.value = CPU2Value;
	MEM_Progress.value  = MEMValue;

	//speefan updating stuff
	Speedfan.Refresh();
	Temp1Value.innerText = getValue(Temp1SensorString);
}

function view_onOptionChanged()
{
	LoadOptions();
}

function LoadOptions()
{
	debug.trace(parseInt(options.count));

	//if(options.exists("Comp1Name")) 
	{
		Temp1Label.innerText = options.getValue("Comp1Name");
		debug.trace(Temp1Label.innerText);
	}
	//else
	{
	//	debug.trace("Comp1Name not found");
	}
	//if(options.exists("Comp1Sensor"))
	{
		Temp1SensorString = options.getValue("Comp1Sensor");
	}
	//else
	{
	//	debug.trace("Comp1Sensor not found");
	}
}

function getValue(id)
{
	switch (id)
	{
	case "empty": return " ";
	case "temp0": return Speedfan.GetTemperature(0) + "°C";
	case "temp1": return Speedfan.GetTemperature(1) + "°C";
	case "temp2": return Speedfan.GetTemperature(2) + "°C";
	case "temp3": return Speedfan.GetTemperature(3) + "°C";
	case "temp4": return Speedfan.GetTemperature(4) + "°C";
	case "temp5": return Speedfan.GetTemperature(5) + "°C";
	case "temp6": return Speedfan.GetTemperature(6) + "°C";
	case "temp7": return Speedfan.GetTemperature(7) + "°C";
	case "temp8": return Speedfan.GetTemperature(8) + "°C";
	case "temp9": return Speedfan.GetTemperature(9) + "°C";
	case "temp10": return Speedfan.GetTemperature(10) + "°C";
	case "temp11": return Speedfan.GetTemperature(11) + "°C";
	case "fan0": return Speedfan.GetFanSpeed(0);
	case "fan1": return Speedfan.GetFanSpeed(1);
	case "fan2": return Speedfan.GetFanSpeed(2);
	case "fan3": return Speedfan.GetFanSpeed(3);
	case "fan4": return Speedfan.GetFanSpeed(4);
	case "fan5": return Speedfan.GetFanSpeed(5);
	case "fan6": return Speedfan.GetFanSpeed(6);
	case "fan7": return Speedfan.GetFanSpeed(7);
	case "fan8": return Speedfan.GetFanSpeed(8);
	case "fan9": return Speedfan.GetFanSpeed(9);
	case "fan10": return Speedfan.GetFanSpeed(10);
	case "fan11": return Speedfan.GetFanSpeed(11);
	case "volt0": return Speedfan.GetVoltage(0) + "V";
	case "volt1": return Speedfan.GetVoltage(1)+ "V";
	case "volt2": return Speedfan.GetVoltage(2)+ "V";
	case "volt3": return Speedfan.GetVoltage(3)+ "V";
	case "volt4": return Speedfan.GetVoltage(4)+ "V";
	case "volt5": return Speedfan.GetVoltage(5)+ "V";
	case "volt6": return Speedfan.GetVoltage(6)+ "V";
	case "volt7": return Speedfan.GetVoltage(7)+ "V";
	case "volt8": return Speedfan.GetVoltage(8)+ "V";
	case "volt9": return Speedfan.GetVoltage(9)+ "V";
	case "volt10": return Speedfan.GetVoltage(10)+ "V";
	case "volt11": return Speedfan.GetVoltage(11)+ "V";
	case "volt12": return Speedfan.GetVoltage(12)+ "V";
	case "volt13": return Speedfan.GetVoltage(13)+ "V";
	case "volt14": return Speedfan.GetVoltage(14)+ "V";
	case "volt15": return Speedfan.GetVoltage(15)+ "V";
	case "volt16": return Speedfan.GetVoltage(16)+ "V";
	case "volt17": return Speedfan.GetVoltage(17)+ "V";
	case "volt18": return Speedfan.GetVoltage(18)+ "V";
	case "volt19": return Speedfan.GetVoltage(19)+ "V";
	}
}

// Register ActiveX component
function RegisterSpeedfan() 
{
	var classRoot = regRoot + "\\Software\\Classes\\SpeedfanReader.Speedfan\\";
	var clsidRoot = regRoot + "\\Software\\Classes\\CLSID\\" + dllCLSID + "\\";

	try
	{
		oShell.RegWrite(classRoot,"SpeedfanReader.Speedfan", "REG_SZ");
		oShell.RegWrite(classRoot + "CLSID\\", dllCLSID, "REG_SZ");
		oShell.RegWrite(clsidRoot, "SpeedfanReader.Speedfan", "REG_SZ");
		oShell.RegWrite(clsidRoot + "InprocServer32\\", "mscoree.dll", "REG_SZ");
		oShell.RegWrite(clsidRoot + "InprocServer32\\ThreadingModel", "Both", "REG_SZ");
		oShell.RegWrite(clsidRoot + "InprocServer32\\Class", "SpeedfanReader.Speedfan", "REG_SZ");
		oShell.RegWrite(clsidRoot + "InprocServer32\\Assembly", "SpeedfanReader, Version=1.0.2588.9125, Culture=neutral, PublicKeyToken=null", "REG_SZ");
		oShell.RegWrite(clsidRoot + "InprocServer32\\RuntimeVersion", "v2.0.50727", "REG_SZ");
		oShell.RegWrite(clsidRoot + "InprocServer32\\CodeBase", "file:///" + gadget.storage.extract("SpeedfanReader.dll"), "REG_SZ");
		oShell.RegWrite(clsidRoot + "InprocServer32\\1.0.2588.9125\\Class", "SpeedfanReader.Speedfan", "REG_SZ");
		oShell.RegWrite(clsidRoot + "InprocServer32\\1.0.2588.9125\\Assembly", "SpeedfanReader, Version=1.0.2588.9125, Culture=neutral, PublicKeyToken=null", "REG_SZ");
		oShell.RegWrite(clsidRoot + "InprocServer32\\1.0.2588.9125\\RuntimeVersion", "v2.0.50727", "REG_SZ");
		oShell.RegWrite(clsidRoot + "InprocServer32\\1.0.2588.9125\\CodeBase", "file:///" + gadget.storage.extract("SpeedfanReader.dll"), "REG_SZ");
		oShell.RegWrite(clsidRoot + "ProgId\\", "SpeedfanReader.Speedfan", "REG_SZ");
		oShell.RegWrite(clsidRoot + "ProgId\\Implemented Categories\\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}\\", "", "REG_SZ");
	}
	catch(err) 
	{
	    debug.trace("RegisterSpeedfan: "+err.name+" - "+err.message)
	}
}

// Unregister ActiveX component
function UnregisterSpeedfan() 
{
	var classRoot = regRoot + "\\Software\\Classes\\SpeedfanReader.Speedfan\\";
	var clsidRoot = regRoot + "\\Software\\Classes\\CLSID\\" + dllCLSID + "\\";

	try
	{		
		oShell.RegDelete(clsidRoot + "ProgId\\Implemented Categories\\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}\\");
		oShell.RegDelete(clsidRoot + "ProgId\\Implemented Categories\\");
		oShell.RegDelete(clsidRoot + "ProgId\\");
		oShell.RegDelete(clsidRoot + "InprocServer32\\1.0.2588.9125\\");
		oShell.RegDelete(clsidRoot + "InprocServer32\\");
		oShell.RegDelete(clsidRoot);
		oShell.RegDelete(classRoot + "CLSID\\");
		oShell.RegDelete(classRoot);
	}
	catch(err) 
	{
	    debug.trace("UnregisterSpeedfan: "+err.name+" - "+err.message);
	}
}

// Try to register the DLL
function activeDLL(root) 
{
	regRoot = root;

	try
	{
		RegisterSpeedfan();
		Speedfan = new ActiveXObject("SpeedfanReader.Speedfan");
	}
	catch(err)
	{
		Speedfan = null;
		debut.trace("activeDLL failed: "+root);
		UnregisterSpeedfan();
		return false;
	}
	return true;
}